package intelligentcity.model.intersections;

import intelligentcity.memoirs.Diary;
import intelligentcity.model.Point;
import intelligentcity.model.Road;
import intelligentcity.model.streets.HorizontalStreet;
import intelligentcity.model.streets.OneWayStreet;
import intelligentcity.model.streets.VerticalStreet;
import intelligentcity.system.IntelligentLights;
import intelligentcity.system.Lights;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Intersection extends Road {

    private final VerticalStreet verticalStreet;
    private final HorizontalStreet horizontalStreet;
    private final List<Point> pointsOfTurn;
    private final List<Point> exitPoints;
    private final List<Point> entryPoints;
    private final Diary diary;

    private Lights lights;

    public Intersection(VerticalStreet verticalStreet, HorizontalStreet horizontalStreet) {

        this.verticalStreet = verticalStreet;
        this.horizontalStreet = horizontalStreet;
        this.pointsOfTurn = new ArrayList<>(4);
        this.exitPoints = new ArrayList<>(4);
        this.entryPoints = new ArrayList<>(4);

        int x1 = verticalStreet.getFirstStreet().getP1().getX();
        int x2 = verticalStreet.getSecondStreet().getP1().getX();
        int y1 = horizontalStreet.getFirstStreet().getP1().getY();
        int y2 = horizontalStreet.getSecondStreet().getP1().getY();

        pointsOfTurn.add(new Point(x1, y1));
        pointsOfTurn.add(new Point(x2, y1));
        pointsOfTurn.add(new Point(x1, y2));
        pointsOfTurn.add(new Point(x2, y2));

        exitPoints.add(verticalStreet.getFirstStreet().getP2());
        exitPoints.add(verticalStreet.getSecondStreet().getP1());
        exitPoints.add(horizontalStreet.getFirstStreet().getP1());
        exitPoints.add(horizontalStreet.getSecondStreet().getP2());

        entryPoints.add(verticalStreet.getFirstStreet().getP1());
        entryPoints.add(verticalStreet.getSecondStreet().getP2());
        entryPoints.add(horizontalStreet.getFirstStreet().getP2());
        entryPoints.add(horizontalStreet.getSecondStreet().getP1());

        this.diary = new Diary();
        diary.start();

    }

    public void setLights(Lights lights) {
        this.lights = lights;
        if (lights instanceof IntelligentLights) {
            ((IntelligentLights) this.lights).setDiary(diary);
        }
        new Thread(this.lights).start();
    }

    @Override
    public String toString() {
        return verticalStreet.toString() + " x " + horizontalStreet.toString();
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Intersection && ((Intersection) o).getHorizontalStreet().equals(horizontalStreet)
                && ((Intersection) o).getVerticalStreet().equals(verticalStreet)) {
            return true;
        }
        return false;
    }

    @Override
    public List<OneWayStreet> getOneWayStreets() {
        List<OneWayStreet> oneWayStreets = new LinkedList<>();
        oneWayStreets.addAll(this.verticalStreet.getOneWayStreets());
        oneWayStreets.addAll(this.horizontalStreet.getOneWayStreets());
        return oneWayStreets;
    }

    @Override
    public List<Point> getPointsOfTurn() {
        return pointsOfTurn;
    }

    public VerticalStreet getVerticalStreet() {
        return verticalStreet;
    }

    public HorizontalStreet getHorizontalStreet() {
        return horizontalStreet;
    }

    public Lights getLights() {
        return lights;
    }

    public Diary getDiary() {
        return diary;
    }

}
